//
//  NetworkManager.swift
//  Checked
//
//  Created by admin on 01/03/2017.
//  Copyright © 2017 shinolr_go. All rights reserved.
//

import UIKit
import Moya

var accessToken: String?

class NetworkManager {

	typealias CallBack = (() -> Void)

	/// 单例全局访问点

    static let sharedManager = NetworkManager()
//    class var sharedManager: NetworkManager {
//        struct singletonWrapper {
//            static let singleton = NetworkManager()
//        }
//        return singletonWrapper.singleton
//    }

	/// 创建 provider
	///
	/// - Parameters:
	///   - target: 请求类型
	///   - log: 请求头 参数
    private func initProvider<T: TargetType>(target: T, header: Bool? = false) -> MoyaProvider<T> {
        var provider: MoyaProvider<T>
        if header! {
            let endpointClosure = { (target: T) -> Endpoint in
                let url = target.baseURL.appendingPathComponent(target.path).absoluteString
                let sampleClosure = {
                     EndpointSampleResponse.networkResponse(200, target.sampleData)
                }

                let point = Endpoint(                  url: url,
                                     sampleResponseClosure: sampleClosure,
                                                    method: target.method,
                                                      task: target.task,
                                          httpHeaderFields: ["Content-Type": "application/json",
                                                            "Authorization": "Bearer \(accessToken!)",
                                                        "trakt-api-version": "2",
                                                            "trakt-api-key": "\(clientID)"])

//                let endpoint = Endpoint(                      url: url,
//                                            sampleResponseClosure: {.networkResponse(200, target.sampleData)},
//                                                           method: target.method,
//                                                       parameters: target.parameters)

                // 添加 AccessToken
//                return endpoint.adding(newHTTPHeaderFields:     ["Content-Type": "application/json",
////                                                                "Authorization": "Bearer \(accessToken)",
//                                                            "trakt-api-version": "2",
//                                                                "trakt-api-key": "\(clientID)"])
                return point
            }
            provider = MoyaProvider<T>(endpointClosure: endpointClosure)
        } else {
            provider = MoyaProvider<T>()
        }

        return provider
    }

	/// 获取 accessToken
	///
	/// - Parameters:
	///   - target: 请求类型
	///   - log: 请求头参数
//    func request<T: TargetType>(target: T, header: Bool? = false, callBack: @escaping (Response?, MoyaError?) -> ()) {
//        let provider = initProvider(target: target, header: header)
//        provider.request(target) { (result) in
//            switch result {
//            case let .success(moyaResponse):
////                print(moyaResponse.response!)
//                callBack(moyaResponse, nil)
//            case let .failure(error):
//                callBack(nil, error)
//            }
//        }
//    }
//
//    func requestMyShows<T: TargetType>(target: T, header: Bool? = false) {
//        let provider = initProvider(target: target, header: header)
//        provider.request(target) { (result) in
//            switch result {
//            case let .success(moyaResponse):
//                let data = moyaResponse.data
//                let json = JSON(data: data)
//                print("json: \(json)")
//            case let .failure(error):
//                print("error: \(error)")
//            }
//        }
//    }
}
